package com.feixiang.feixiangagent.proxy;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.MessageType;
import org.springframework.ai.chat.messages.UserMessage;
import java.util.HashMap;
import java.util.Map;

/**
 * @author feixiang
 * 自定义消息转换器(转换Spring AI的Message,默认不支持序列化)
 */
public class MessageConverter {
    // 将 Message 转换为可序列化的 Map
    public static Map<String, Object> toSerializableMap(Message message) {
        Map<String, Object> map = new HashMap<>();
        map.put("type", message.getMessageType().name());
        map.put("text", message.getText());
        map.put("metadata", message.getMetadata());
        return map;
    }
    // 从 Map 反序列化为 Message
    public static Message fromSerializableMap(Map<String, Object> map) {
        String type = (String) map.get("type");
        String text = (String) map.get("text");
        Map<String, Object> metadata = (Map<String, Object>) map.get("metadata");
        switch (MessageType.valueOf(type)) {
            case USER:
                return new UserMessage(text);
            case ASSISTANT:
                return new AssistantMessage(text, metadata);
            default:
                throw new IllegalArgumentException("Unsupported message type: " + type);
        }
    }
}
